<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];

include $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();
$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}


$mode=$_GET['mode'];
if (isset($mode) && $mode == "search"){
	$foodName = $_GET['food_name'];
	$listFood = $managerIUDS->listQueryLimit($page, "food", "food_id"," WHERE food_name = '".$foodName."'");
}else {
	$listFood = $managerIUDS->listQueryLimit($page, "food", "food_id","");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Food List :.</title>
<?php
require_once("../includes/css_inc.php");
require_once("../includes/js_inc.php");
?>
<script>
	var objData = new Object();
	var breedData = new Object();
			$(document).ready(function(){ 
				$("#insert").click(function(){ 
					window.location = "food_detail.php?mode=insert";
				}); 
				$( "#food_name" ).autocomplete({
					source: function( request, response ) {
		                $.ajax({
							type: "GET",
							url: "../actions/food_action.php",
							data: {mode : "listfoodData",food_name : $("#food_name").val()},
							contentType: "application/json; charset=utf-8", 
							dataType: "json",
		                    success: function( data ) {
			                        response( $.map( data, function( item ) {
			                            return {
			                                label: item.food_name,
			                                value: item.food_name
			                            }
			                        }));
		                    }
		                });
		            },
					minLength: 2
				});
			}); 
			function deleteFoodData(foodId) {
				if (confirm("Do you really want to delete?")) {
					window.location = "../actions/food_action.php?mode=delete&food_id=" + foodId;
				}
			}
			function updateFoodData(foodId) {
					window.location = "food_detail.php?mode=update&food_id=" + foodId;
			}
			 
		</script>
</head>
<body>
<form name="frm1" id="frm1" action="../actions/food_action.php" method="get">
	<table width="100%" border="0">
		<tr>
        	<td align="right"> ชื่ออาหาร : 
        		<input type="text" name="food_name" id="food_name" class="higthInput">
				<input type="submit" value="ค้นหา"/>
			</td>
         </tr>	
	</table>
	<input type="hidden" name="mode" id="mode" value="search">
</form>
<table style="width: 100%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<td width="2%" align="center">ลำดับ</td>
			<td width="3%" align="center">ซื่ออาหาร</td>
			<td width="10%" align="center">รายละเอียด</td>
			<td width="3%" align="center">ประเภท</td>
			<td width="3%" align="center">ราคา</td>
			<td width="3%" align="center">น้ำหนัก</td>
			<td width="3%" align="center">แก้ไข</td>
			<td width="3%" align="center">ลบ</td>
		</tr>
	</thead>
	<tbody class="ui-widget-content">
	<?php
	$i = $listFood->startLimit;
	$r = 0;
	$i = ((($page*10)+1)-10);
	if(sizeof($listFood) > 0){
		foreach ($listFood as $value) {
			?>
		<tr>
			<td width="2%" align="center"><?=$i?></td>
			<td width="3%" align="center"><?=$value["food_name"]?></td>
			<td width="10%" align="center"><?=$value["food_detail"]?></td>
			<td width="5%" align="center"><?php 
			$speciesData = $managerIUDS->queryData("species_name", "species", " WHERE species_id='".$value["species_id"]."'", "1", "");
			echo $speciesData["species_name"];
			?></td>
			<td width="10%" align="center"><?=$value["price"]?></td>
			<td width="5%" align="center"><?php
			if($value["pet_weight"] =="L"){
				echo "น้อยกว่า 25";
			}else{
				echo "มากกว่า 25";
			}
			?></td>
			<!--<td width="10%" align="center"><?=$value["species_name"]?></td>
           	   					<td width="20%" align="center"><?=$value["species_detail"]?></td>-->
			<td width="3%" align="center">
			<button onclick="updateFoodData('<?=$value["food_id"]?>');">
				<span class="ui-icon ui-icon-pencil" title="edit"></span>
			</button>
			</td>
			<td width="3%" align="center">
			<button onclick="deleteFoodData('<?=$value["food_id"]?>');">
				<span class="ui-icon ui-icon-trash" title="delete"></span>
			</button>
			</td>
		</tr>
		<?php
		$i++; $r++;}
		}else{
			if($mode!="search"){
			?>
			<tr>
				<td colspan="9" align="center"><font size="2" color="red">ไม่มีข้อมูล!!!</font></td>
			</tr>
			<?php
			}else{
			?>
			<tr>
				<td colspan="9" align="center"><font size="2" color="red">ไม่พบข้อมูล</font></td>
			</tr>
			<?php
			}
		}
		?>
		<tr>
			<td align="center" colspan="11"><?=$managerIUDS->buildPageBtn($page, $managerIUDS->getMaxPageByColumnName("food", "food_id"))?></td>
		</tr>
		<tr>
			<td align="center" colspan="11"><input type="button" name="insert"
				id="insert" value="เพิ่มข้อมูล"></td>
		</tr>
	</tbody>
</table>
</body>
</html>
